package com.b1502.store2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.b1502.store2.Bean.CategoryListBean;
import com.b1502.store2.R;
import com.b1502.store2.util.UrlUtil;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by zhangdongxu on 2017/2/28.
 */

public class CategroyListAdapter extends RecyclerView.Adapter<CategroyListAdapter.MyViewHolder>implements View.OnClickListener,View.OnLongClickListener{


    private addOncli mOncli = null;
    private Context context;
    private List<CategoryListBean.ProductsBean> categroylist;

    @Override
    public boolean onLongClick(View view) {
        mOncli.onLongClick(view, (CategoryListBean.ProductsBean) view.getTag());


        return false;
    }


    //define interface
    public interface addOncli {
        void onItemClick(View view , CategoryListBean.ProductsBean data);
        void onLongClick(View view , CategoryListBean.ProductsBean data);
    }

    public CategroyListAdapter(Context context, List<CategoryListBean.ProductsBean> categroylist) {

        this.context = context;
        this.categroylist = categroylist;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context,R.layout.categroylist_item,null);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.fl_list_tv.setText(categroylist.get(position).getName());
        holder.fl_list_price.setText("￥"+categroylist.get(position).getPrice());
        Glide.with(context).load(UrlUtil.getImageUrl(categroylist.get(position).getImageUrl())).into(holder.fl_list_img);
        holder.itemView.setTag(categroylist.get(position));

    }

    @Override
    public int getItemCount() {
        return categroylist.size();
    }

    @Override
    public void onClick(View view) {
            mOncli.onItemClick(view, (CategoryListBean.ProductsBean) view.getTag());

        }
    public void setOnclick(addOncli listener){
        this.mOncli = listener;
    }
    public void setLoingOnclick(addOncli lister){
        this.mOncli =lister;
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView fl_list_tv;
        private ImageView fl_list_img;
        private TextView  fl_list_price;
        public MyViewHolder(View itemView) {
            super(itemView);
            fl_list_tv  = (TextView) itemView.findViewById(R.id.fl_list_tv);
            fl_list_img = (ImageView) itemView.findViewById(R.id.fl_list_img);
            fl_list_price = (TextView) itemView.findViewById(R.id.fl_list_price);
        }
    }


}